//k1编辑 2019.9.24

if (!isNil "VCMINITHANDLE" && {!scriptDone VCMINITHANDLE}) exitWith {};

//全局操作编译
Vcm_PMN = compileFinal "(_this select 0) playMoveNow (_this select 1);";
Vcm_SM = compileFinal "(_this select 0) switchMove (_this select 1);";
Vcm_PAN = compileFinal "(_this select 0) playActionNow (_this select 1);";
VCM_PublicScript = compileFinal "[] call (_this select 0);";
VCM_ServerAsk = compileFinal "if (isServer) then {publicvariable (_this select 0);};";

VCM_remoteExec_1 = {
	params ["_ne","_unit"];
	if (local _ne) then {
		private _kv = _ne knowsAbout _unit;
		_ne reveal [_unit,(_kv + 0.4)];
	};	
};//

/*
Reasoning for removal: It's best to have Vcom running for ALL clients. Vcom was designed from the ground up to ONLY run on AI LOCAL to the machine running the commands. This enables HC support, hotswapping of AI between CPUS, and better consistency over several hours with multiple disconnects/changes of HC's or AI owners.

if 
!(
	isServer || 
	!hasInterface || 
	allCurators findIf {getAssignedCuratorUnit _x == player} == -1
) exitWith {};
*/

//Parameters
[] call compile preprocessFileLineNumbers "vcomai\Vcom\Functions\VCOMAI_DefaultSettings.sqf"; //Load default settings
/*if (isFilePatchingEnabled && {"" != loadFile "\userconfig\VCOM_AI\AISettingsV4.hpp"}) then
{
	[] call compile preprocessFileLineNumbers "\userconfig\VCOM_AI\AISettingsV4.hpp"; //Overwrite with userconfig
};*/
if (isClass (configfile >> "CfgPatches" >> "cba_settings")) then {[] call VCM_fnc_CBA_Settings}; //Overwrite with CBA settings

//Mod checks
//ACE CHECK
if (not isNil "ACE_Medical_enableFor" && {ACE_Medical_enableFor isEqualTo 1}) then {VCM_MEDICALACTIVE = false;} else {VCM_MEDICALACTIVE = true;};

VCOM_MINEARRAY = [];
[] spawn VCM_fnc_MineMonitor;
[] spawn VCM_fnc_HANDLECURATORS;

VcmAI_ActiveList = [];//
VCMINITHANDLE = [] spawn
{
	waitUntil {time > 2};
	sleep 2;
	
	//Begin Artillery function created by Rydygier - https://forums.bohemia.net/forums/topic/159152-fire-for-effect-the-god-of-war-smart-simple-ai-artillery/
	if (VCM_FFEARTILLERY) then {VCM_FFEHANDLE = [] spawn (compile preprocessFileLineNumbers "vcomai\Vcom\RYD_FFE\FFE.sqf");VCM_ARTYENABLE = false;};
	
	[] spawn VCM_fnc_AIDRIVEBEHAVIOR;
	
	//下面的循环检查是否有新的AI生成要添加到列表中
	//findIf 游戏v1.82才支持照顾老版本进行了替换，效率稍微降低
	
	while {true} do 
	{
		waitUntil {Vcm_ActivateAI};
		
		{
			private _fsmHandle = _x getVariable "VCM_SQUADFSM";
			if 
			(
				(isNil {completedFSM _fsmHandle} || {completedFSM _fsmHandle}) && 
				{local _x} && 
				{simulationEnabled (leader _x)} && 
				{
					private _idx = -1;//
					{if(isPlayer _x)exitWith{_idx=_forEachIndex}} forEach units _x;//
					_idx isEqualTo -1
				} && //{(units _x) findIf {isPlayer _x} isEqualTo -1}
				{(leader _x) isKindOf "Man"}
			) then {
				private _grp = _x;
				private _idx = -1;//
				{if(alive _x)exitWith{_idx=_forEachIndex}} forEach units _grp;//
				if !(_idx isEqualTo -1) then //_idx: ((units _grp) findIf {alive _x})
				{
					_grp spawn VCM_fnc_SQUADBEH;
				};
			};
		} foreach allGroups;
		
		
		//这个系统绝对是一个很酷的主意-但是运行一个fsm每AI是沉重的。我想进一步研究性能影响。
		if (VCM_SUPPRESSACTIVE) then
		{
			{
				private _fsmHandle = _x getVariable "VCMSUPPRESSION";
				if
				(
					(isNil {completedFSM _fsmHandle} || {completedFSM _fsmHandle}) &&
					{isNull objectParent _x} &&
					{local _x} &&
					{simulationEnabled _x} &&
					{!isPlayer _x} &&
					{_x isKindOf "Man"} &&
					{alive _x}
				) then
				{
					_x spawn VCM_fnc_UNITSUPPRESSION; //对其他医疗模组有影响，倒下的AI会移动
				};
			} foreach allUnits;
		};
		
		sleep 15;
	};
};